package com.sdtele.tta.operation;

import java.sql.Timestamp;

import com.sdtele.tta.dac.Dac;
import com.sdtele.tta.models.JsonRequest;
import com.sdtele.tta.models.JsonResponse;
import com.sdtele.tta.models.Task;
import com.sdtele.tta.models.User;

/**
 * 操作类实现：GetUser(json.operation=getUser)
 * 
 * @author XCream
 */
public class OperationSetCompletion extends Operation {

	@Override
	public JsonResponse handle(JsonRequest jsonRequest) {
		JsonResponse jres = new JsonResponse();
		User curUser = (User) request.getSession().getAttribute("user");

		long taskId = jsonRequest.getJsonObject().get("taskId").getAsLong();
		int completion = jsonRequest.getJsonObject().get("completion").getAsInt();

		Task task = Dac.getInstance().getTaskbyTaskid(taskId);
		if ((Dac.getInstance().getOperation(taskId, curUser.getId()).getOperation()) !=2) {
			if (completion == -1 && (task.getCompletion() >= 100)) {
				task.setCompletion(task.getCompletion() - 100);
				Timestamp updateTime = new Timestamp(System.currentTimeMillis());
				task.setUpdateTime(updateTime);
			} else if (completion == 1000 && (task.getCompletion() < 100)) {
				task.setCompletion(task.getCompletion() + 100);
				Timestamp updateTime = new Timestamp(System.currentTimeMillis());
				task.setUpdateTime(updateTime);

			} else if (completion >= 0 && completion <= 100) {
				task.setCompletion(completion);
				Timestamp updateTime = new Timestamp(System.currentTimeMillis());
				task.setUpdateTime(updateTime);
			}

			System.out.println(task);
			if (Dac.getInstance().chgCompletionbyTaskid(task))

			{
				jres.setStatus(0);
				jres.setMessage("修改成功");
				jres.setEntity(task);
			}

		} else {
			jres.setStatus(1);
			jres.setMessage("用户无权修改任务完成度");
		}

		return jres;
	}

}
